/********************************************
 * 功能说明: TenantEmployee实体与DTO之间接口数据转换实现
 * 模块名称:
 * 系统名称:
 * 软件版权: Frank
 * 系统版本: 1.0.0.1
 * 开发人员:
 * 开发时间: 2019-12-18 18:41:08
 * 审核人员:
 * 相关文档:
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.uaa.web.converter;

import com.spring.arch.common.converter.AbstractConverter;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.uaa.enums.CreateType;
import com.spring.arch.uaa.errorcode.UaaErrorCode;
import com.spring.arch.uaa.web.dto.WebTenantEmployeeDTO;
import com.spring.arch.common.converter.AbstractConverter;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.uaa.enums.CreateType;
import com.spring.arch.uaa.errorcode.UaaErrorCode;
import com.spring.arch.uaa.model.TenantEmployee;
import com.spring.arch.uaa.repository.TenantEmployeeRepository;
import com.spring.arch.uaa.repository.TenantRepository;
import com.spring.arch.uaa.web.dto.WebTenantEmployeeDTO;
import lombok.NonNull;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;

/**
 * TenantEmployee实体与DTO之间接口数据转换实现
 *
 * @author
 * @version 1.0.0.1
 * @since 2019-12-18 18:41:08
 */
@Component
public class WebTenantEmployeeConverter extends AbstractConverter<TenantEmployee, WebTenantEmployeeDTO> {

    @Autowired
    private TenantEmployeeRepository tenantEmployeeRepository;

    @Autowired
    private TenantRepository tenantRepository;

    @Autowired
    private EntityManager entityManager;

    @Override
    public TenantEmployee toModel(@NonNull final WebTenantEmployeeDTO dto) {
        if(dto.isNew()) {
            return constructModel(dto);
        } else {
            return updateModel(dto);
        }
    }

    @Override
    public WebTenantEmployeeDTO toDTO(@NonNull final TenantEmployee model) {
        final WebTenantEmployeeDTO dto = new WebTenantEmployeeDTO();

        dto.setId(model.getId());
        dto.setCode(model.getCode());
        dto.setName(model.getName());
        dto.setMobile(model.getMobile());
        dto.setGender(model.getGender());
        dto.setEmail(model.getEmail());
        dto.setPhotos(model.getPhotos());
        dto.setStatus(model.isStatus());

        this.convertCommonToDTO(model, dto);

        return dto;
    }

    /**
     * 构建新Model
     */
    private TenantEmployee constructModel(final WebTenantEmployeeDTO dto) {

        final TenantEmployee model = new TenantEmployee();
        model.setTenant(tenantRepository.findById(dto.getTenantId()).orElseThrow(() ->
                ExceptionHolder.serviceException(UaaErrorCode.E2001001001, dto.getTenantId().toString())));
        this.dtoToModel(dto, model);
        return model;
    }

    /**
     * 更新Model
     */
    private TenantEmployee updateModel(final WebTenantEmployeeDTO dto) {

        final TenantEmployee model = this.tenantEmployeeRepository.findById(dto.getId())
                .orElseThrow(() -> ExceptionHolder.serviceException(UaaErrorCode.E2001001002,
                        dto.getId().toString()));

        entityManager.unwrap(Session.class).evict(model);
        this.dtoToModel(dto, model);
        return model;
    }

    private void dtoToModel(final WebTenantEmployeeDTO dto, final TenantEmployee model) {

        model.setCode(dto.getCode());
        model.setName(dto.getName());
        model.setMobile(dto.getMobile());
        model.setGender(dto.getGender());
        model.setEmail(dto.getEmail());
        model.setPhotos(dto.getPhotos());
        model.setCreateType(CreateType.SAVE);
        model.setStatus(dto.isStatus());
    }
}
